package com.furongji.education.controller.api;


import com.furongji.common.exception.MyException;
import com.furongji.common.result.Result;
import com.furongji.common.utils.RedissonLockUtil;
import com.furongji.education.domain.po.Order;
import com.furongji.education.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author furongji
 * @since 2025-05-29
 */
@Tag(name = "订单管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/orders")
public class OrderApiController {

    private final OrderService orderService;

    @Operation(summary = "生成订单")
    @PostMapping("/createOrder/{courseId}")
    public Result createOrder(@RequestHeader("Authorization") String token, @PathVariable String courseId) {
        String orderId = orderService.createOrder(token, courseId);
        return Result.success(orderId);
    }

    @Operation(summary = "获取订单详细信息")
    @GetMapping("/getOrderInfo/{orderId}")
    public Result getOrderInfo(@PathVariable String orderId) {
        Order order = orderService.getById(orderId);
        return Result.success(order);
    }

    // feign
    @Operation(summary = "查询用户是否已经购买过该课程")
    @GetMapping("/isPurchaseCourse/{userId}/{courseId}")
    public Result isPurchaseCourse(@PathVariable String userId, @PathVariable String courseId) {
        System.err.println("order-service: ....  userId: " + userId + "courseId：" + courseId);
        boolean isPurchase = orderService.isPurchaseCourse(userId, courseId);
        return isPurchase ? Result.success() : Result.error();
    }
}
